<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 该页面已完成
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input
          clearable
          v-model="query.templateId"
          placeholder="模版Id"
          class="handle-input mr10"
          @keyup.native.enter="handleSearch()"
        ></el-input>
        <el-input
          clearable
          v-model="query.mailTo"
          placeholder="收件人"
          class="handle-input mr10"
          @keyup.native.enter="handleSearch()"
        ></el-input>
        <el-select
          clearable
          v-model="query.status"
          placeholder="状态"
          class="handle-select mr10"
        >
          <el-option key="1" label="发送成功" value="1"></el-option>
          <el-option key="2" label="发送失败" value="0"></el-option>
        </el-select>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button
          type="danger"
          class="handle-del"
          @click="delAllSelection"
          v-if="mmlPermissions.includes('sys:maillog:delete')"
          >删除</el-button
        >
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="templateId"
          label="模版ID"
          width="55"
          align="center"
        ></el-table-column>
        <el-table-column prop="mailFrom" label="发送者"></el-table-column>
        <el-table-column prop="mailTo" label="收件人"></el-table-column>
        <el-table-column prop="mailCc" label="抄送"></el-table-column>
        <el-table-column prop="subject" label="主题"></el-table-column>

        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tag
              :type="
                scope.row.status === '发送成功'
                  ? 'success'
                  : scope.row.status === '发送失败'
                  ? 'danger'
                  : ''
              "
              >{{ scope.row.status }}</el-tag
            >
          </template>
        </el-table-column>

        <el-table-column prop="createDate" label="发送时间"></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
              v-if="mmlPermissions.includes('sys:maillog:delete')"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total,sizes, prev, pager, next, jumper"
          :page-sizes="[5, 10, 20, 30]"
          @size-change="handleSizeChange"
          :current-page="query.pageIndex"
          :page-size="query.pageSize"
          :total="pageTotal"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
// import { messageMailLogMixin } from "../../common/js/messageManage";
import util from "utils/util";
import { mapGetters } from "vuex";
export default {
  name: "messagemaillog",
  // mixins: [messageMailLogMixin],

  data() {
    return {
      mmlPermissions: [],
      query: {
        templateId: "",
        mailTo: "",
        status: "",
        pageIndex: 1,
        pageSize: 20,
      },
      tableData: [
        {
          id: "",
          templateId: "",
          mailFrom: "",
          mailTo: "",
          mailCc: "",
          subject: "",
          content: "",
          status: "",
          createDate: "",
        },
      ],
      multipleSelection: [],
      delList: [],
      pageTotal: 0,
      form: {},
      idx: -1,
      ids: [],
    };
  },
  methods: {
    // 获取数据
    getMMLData({
      order = "",
      orderField = "",
      page = this.query.pageIndex,
      limit = this.query.pageSize,
      templateId = this.query.templateId,
      mailTo = this.query.mailTo,
      _t = Date.now(),
      status = this.query.status,
    }) {
      this.$axios
        .get("/sys/maillog/page", {
          params: {
            order,
            orderField,
            page,
            limit,
            templateId,
            mailTo,
            _t,
            status,
          },
        })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return;
          }
          // console.log(res);
          this.pageTotal = res.data.total || 0;
          this.tableData = res.data.list;
          this.tableData.map((item) => {
            item.status = item.status == 1 ? "发送成功" : "发送失败";
          });
        })
        .catch(() => {});
    },
    // 触发搜索按钮
    handleSearch() {
      this.$set(this.query, "pageIndex", 1);
      this.getMMLData({
        templateId: this.query.templateId,
        mailTo: this.query.mailTo,
        status: this.query.status,
      });
    },
    // 删除操作
    handleDelete(index, row) {
      this.ids[0] = row.id;
      util.delete.call(this, "/sys/maillog", index, this.ids, this.getMMLData);
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 批量删除次
    delAllSelection() {
      var ids = this.multipleSelection.map((item) => {
        return item.id;
      });
      util.delete.call(this, "/sys/maillog", "", ids, this.getMMLData);
    },

    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, "pageIndex", val);
      this.getMMLData({});
    },
    handleSizeChange(val) {
      this.$set(this.query, "pageSize", val);
      this.getMMLData({});
    },
  },
  created() {
    this.getMMLData({});
    this.mmlPermissions = this.permissions;
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  watch: {
    "$route.path": function(newVal, oldVal) {
      if (newVal == "/message-mail-log") {
        this.mmlPermissions = this.permissions;
      }
    },
    // 刷新浏览器时触发
    permissions: function(newVal, oldVal) {
      // console.log(newVal, oldVal);
      this.mmlPermissions = newVal;
    },
  },
};
</script>

<style scoped>
@import url("../../common/css/index.css");
</style>
